//
//  RankingView.swift
//  JTYP
//
//  Created by Groot on 2017/12/6.
//  Copyright © 2017年 inspur. All rights reserved.
//

import Alamofire

struct Ranking {
    var title: String?
    var value: String?
    var iconName: String?
    init(title: String?, value: String?, iconName: String?) {
        self.title = title
        self.value = value
        self.iconName = iconName
    }
}

class RankingView: UIView {
    
    var rank: Ranking?
    {
        didSet {
            titleLab.text = rank?.title
            valueLab.text = rank?.value
            iconView.image = UIImage.init(named: rank?.iconName ?? "sport_jinri")
        }
    }
    
    @IBOutlet weak var valueLab: UILabel!
    @IBOutlet weak var titleLab: UILabel!
    @IBOutlet weak var iconView: UIImageView!
    
    override func awakeFromNib() {
        request()
    }
    
    func request() {
        requestReset()
        
        NetWorkUtil.shareInstance.postForObject(objectType: ObjResponse<Rank>.self, urlString: Server.rank, finishCallBack:({ [unowned self](obj, error) in
            
            guard let result = obj, let resultObj = result.data else {
                return
            }
            if self.rank?.title == "今日排行" {
                self.rank?.value = "\(resultObj.dayrank ?? 0)"
            }else {
                self.rank?.value = "\(resultObj.monthrank ?? 0)"
            }
        }))
    }
    
    func requestReset() {
        self.valueLab.text = "-"
    }
}
